#include "../lib/token.h"
#include "../lib/parse.h"
#include "../lib/convert.h"
#include "../lib/custom.h"
#include "../lib/step.h"
#include "../lib/filename.h"
#include "../lib/rename.h"
#include "../lib/args.h"
#include <string>
#include <vector>
#include <iostream>
using namespace std;

void run(string path,string input_parttern,string local_variable,string step_variable,string output_parttern);
void help();



int main(int argc, char** argv) {

    if (argc == 1) {

        // interact
        string path, input_parttern, local_variable, step_variable, output_parttern;

        cout << "input path:\t\t";
        getline(cin, path);
        cout << "#" << path << "#" << endl;

        cout << "input parttern:\t\t";
        getline(cin, input_parttern);
        cout << "#" << input_parttern<< "#" << endl;

        cout << "local variable:\t\t";
        getline(cin, local_variable);
        cout << "#" << local_variable<< "#" << endl;

        cout << "step variable:\t\t";
        getline(cin, step_variable);
        cout << "#" << step_variable<< "#" << endl;

        cout << "output parttern:\t";
        getline(cin, output_parttern);
        cout << "#" << output_parttern<< "#" << endl;

        run(path, input_parttern, local_variable, step_variable, output_parttern);
        
        cout << path << '#' 
            << input_parttern << '#' 
            << local_variable << '#' 
            << step_variable << '#' 
            << output_parttern << endl;

    } else if (argc == 2) {

        string input_args(argv[1]);

        if(!input_args.compare("-h") || !input_args.compare("--help")) {
            help();
        } else { // script

            // path#inputPart#localVari#stepVari#outputPart
            auto args_vec = args_treatment(input_args);
            string path = args_vec[0], input_parttern = args_vec[1], local_variable = args_vec[2], step_variable = args_vec[3], output_parttern = args_vec[4];
        run(path, input_parttern, local_variable, step_variable, output_parttern);

        }

    } else {
        cout << "too many arguments, read readme please.";
    }
    return 0;
}

void run(string path,
        string input_parttern,
        string local_variable,
        string step_variable,
        string output_parttern) {

   // input pattern turns into input tokens
   vector<token> input_tokens = scan_tokens(input_parttern);

   // output pattern turns into output tokens
   vector<token> output_tokens = scan_tokens(output_parttern);

   // stp set step variables
   stepper stp(step_variable);

   // lps set init values of local variables
   local_parser lps(local_variable);
   lps.local_parse();
   vector<variable> local_variables = lps.get_variables();

   
   // get filenames in it (non-recursion)
   vector<string> input_filenames = get_filenames(path);

   for(auto input_filename:input_filenames) {
        try {
           // extract variable in file name by tokens
           vector<variable> variables = parse(input_tokens, input_filename);

           // merge local vari and file vari
           variables.insert(variables.end(), local_variables.begin(), local_variables.end());


           // step variables
           stp.set_variables(variables);
           stp.increment();
           variables = stp.get_variables();
           local_variables.assign(variables.end() - local_variables.size(), variables.end());

           // output filename
           string output_filename = convert(variables, output_tokens);
           rename_file(path + string("/") + input_filename, path + string("/")+ output_filename);

       } catch (string& error) {
           cout << endl;
           cout << input_filename << " cannot parse:" << error << endl;
       }
   }
}


void help() {
    cout << "read readme plz" << endl;
}
